package com.guxingke.typecho4j.jfinal.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;

public class User extends Model<User> {
	private static final long serialVersionUID = 2831631066893213843L;
	public static final User dao = new User();

	public List<User> findAll() {
		return find("select * from typecho_users");
	}

	public User login(String name, String password) {
		return findFirst(
				"SELECT uid,name,screenName,url,mail,`group` FROM typecho_users where name = ? and password = ?",
				name, password);
	}

	public Long findUserIdByUsername(String screenName) {
		User user = findFirst(
				"select uid from typecho_users where screenName=?",
				screenName);
		return user.getLong("uid");
	}

	public User getAuthorByUserId(Long authorId) {
		return User.dao.findById(authorId);
	}

	@Override
	public <T> T get(String attr) {
		if ("password".equals(attr)) {
			return null;
		}
		return super.get(attr);
	}

	public List<User> getUsers() {
		return find("select * from typecho_users");
	}
}
